package cn.iocoder.yudao.module.handguard.dal.mysql.housingresource;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.handguard.dal.dataobject.housingresource.HousingResourceDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.handguard.controller.admin.housingresource.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 房源信息 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface HousingResourceMapper extends BaseMapperX<HousingResourceDO> {

    default PageResult<HousingResourceDO> selectPage(HousingResourcePageReqVO reqVO) {
        String areas = null;
        Integer areaOne = null,areaTwo = null;
        if (StringUtils.isNotEmpty(reqVO.getArea())){
            if (reqVO.getArea().contains("-")){
                List<String> areaList = Arrays.asList(reqVO.getArea().split("-"));
                areaOne = Integer.valueOf(areaList.get(0));
                areaTwo = Integer.valueOf(areaList.get(1));
            }else {
                areas = reqVO.getArea();
            }
        }

        String marketPrices = null;
        Integer marketPriceOne = null,marketPriceTwo = null;
        if (StringUtils.isNotEmpty(reqVO.getMarketPrice())){
            if (reqVO.getMarketPrice().contains("-")){
                List<String> marketPriceList = Arrays.asList(reqVO.getMarketPrice().split("-"));
                marketPriceOne = Integer.valueOf(marketPriceList.get(0));
                marketPriceTwo = Integer.valueOf(marketPriceList.get(1));
            }else {
                marketPrices = reqVO.getMarketPrice();
            }
        }

        LambdaQueryWrapperX<HousingResourceDO> ruList = new LambdaQueryWrapperX<>();
        ruList.eqIfPresent(HousingResourceDO::getArea,areas);
        ruList.betweenIfPresent(HousingResourceDO::getArea,areaOne,areaTwo);
        ruList.eqIfPresent(HousingResourceDO::getHouseType, reqVO.getHouseType());
        ruList.eqIfPresent(HousingResourceDO::getHousingType, reqVO.getHousingType());
        ruList.eqIfPresent(HousingResourceDO::getAuctionNumber, reqVO.getAuctionNumber());
        ruList.betweenIfPresent(HousingResourceDO::getStartingPrice, marketPriceOne,marketPriceTwo);
        ruList.eqIfPresent(HousingResourceDO::getMarketUnitPrice, reqVO.getMarketUnitPrice());
        ruList.eqIfPresent(HousingResourceDO::getPickupSpacePrice, reqVO.getPickupSpacePrice());
        ruList.eqIfPresent(HousingResourceDO::getStartingPrice, marketPrices);
        ruList.eqIfPresent(HousingResourceDO::getStartingOddPrice, reqVO.getStartingOddPrice());
        ruList.eqIfPresent(HousingResourceDO::getDiscount, reqVO.getDiscount());
        ruList.likeIfPresent(HousingResourceDO::getCellName, reqVO.getCellName());
        ruList.eqIfPresent(HousingResourceDO::getStep, reqVO.getStep());
        ruList.eqIfPresent(HousingResourceDO::getFloor, reqVO.getFloor());
        ruList.eqIfPresent(HousingResourceDO::getPropertyType, reqVO.getPropertyType());
        ruList.eqIfPresent(HousingResourceDO::getNumberOfAuctions, reqVO.getNumberOfAuctions());
        ruList.eqIfPresent(HousingResourceDO::getEarnestMoney, reqVO.getEarnestMoney());
        ruList.eqIfPresent(HousingResourceDO::getBidIncrement, reqVO.getBidIncrement());
        ruList.eqIfPresent(HousingResourceDO::getDiscountRate, reqVO.getDiscountRate());
        ruList.eqIfPresent(HousingResourceDO::getArchitectureYear, reqVO.getArchitectureYear());
        ruList.eqIfPresent(HousingResourceDO::getDecorateSituation, reqVO.getDecorateSituation());
        ruList.likeIfPresent(HousingResourceDO::getAddress, reqVO.getAddress());
        ruList.eqIfPresent(HousingResourceDO::getDistrict, reqVO.getDistrict());
        ruList.eqIfPresent(HousingResourceDO::getLongitude, reqVO.getLongitude());
        ruList.eqIfPresent(HousingResourceDO::getLatitude, reqVO.getLatitude());
        ruList.eqIfPresent(HousingResourceDO::getDetailPic, reqVO.getDetailPic());
        ruList.eqIfPresent(HousingResourceDO::getCoverPic, reqVO.getCoverPic());
        ruList.eqIfPresent(HousingResourceDO::getBackgroundPic, reqVO.getBackgroundPic());
        ruList.eqIfPresent(HousingResourceDO::getAuctionState,reqVO.getAuctionState());
        ruList.eqIfPresent(HousingResourceDO::getIsHutai, reqVO.getIsHutai());
        ruList.betweenIfPresent(HousingResourceDO::getCreateTime, reqVO.getCreateTime());
        if (StringUtils.isNotEmpty(reqVO.getTitle())){
            ruList.like(HousingResourceDO::getTitle, reqVO.getTitle()).or().like(HousingResourceDO::getCellName, reqVO.getTitle());
        }
        if (Objects.nonNull(reqVO.getSource()) && reqVO.getSource().equals(1)) {
            ruList.and(i -> i.isNotNull(HousingResourceDO::getBackgroundPic).or(j -> j.isNotNull(HousingResourceDO::getCoverPic)));
        }
        ruList.orderByDesc(HousingResourceDO::getId, HousingResourceDO::getAuctionTime);
        return selectPage(reqVO, ruList);
//        return selectPage(reqVO, new LambdaQueryWrapperX<HousingResourceDO>()
//                .eqIfPresent(HousingResourceDO::getArea,areas)
//                .betweenIfPresent(HousingResourceDO::getArea,areaOne,areaTwo)
//                .eqIfPresent(HousingResourceDO::getHouseType, reqVO.getHouseType())
//                .eqIfPresent(HousingResourceDO::getAuctionNumber, reqVO.getAuctionNumber())
////                .eqIfPresent(HousingResourceDO::getMarketPrice, marketPrices)
//                .betweenIfPresent(HousingResourceDO::getStartingPrice, marketPriceOne,marketPriceTwo)
//                .eqIfPresent(HousingResourceDO::getMarketUnitPrice, reqVO.getMarketUnitPrice())
//                .eqIfPresent(HousingResourceDO::getPickupSpacePrice, reqVO.getPickupSpacePrice())
//                .eqIfPresent(HousingResourceDO::getStartingPrice, marketPrices)
//                .eqIfPresent(HousingResourceDO::getStartingOddPrice, reqVO.getStartingOddPrice())
//                .eqIfPresent(HousingResourceDO::getDiscount, reqVO.getDiscount())
//                .likeIfPresent(HousingResourceDO::getCellName, reqVO.getCellName())
//                .eqIfPresent(HousingResourceDO::getStep, reqVO.getStep())
//                .eqIfPresent(HousingResourceDO::getFloor, reqVO.getFloor())
//                .eqIfPresent(HousingResourceDO::getPropertyType, reqVO.getPropertyType())
//                .eqIfPresent(HousingResourceDO::getNumberOfAuctions, reqVO.getNumberOfAuctions())
//                .eqIfPresent(HousingResourceDO::getEarnestMoney, reqVO.getEarnestMoney())
//                .eqIfPresent(HousingResourceDO::getBidIncrement, reqVO.getBidIncrement())
//                .eqIfPresent(HousingResourceDO::getDiscountRate, reqVO.getDiscountRate())
//                .eqIfPresent(HousingResourceDO::getArchitectureYear, reqVO.getArchitectureYear())
//                .eqIfPresent(HousingResourceDO::getDecorateSituation, reqVO.getDecorateSituation())
//                .likeIfPresent(HousingResourceDO::getAddress, reqVO.getAddress())
//                .eqIfPresent(HousingResourceDO::getDistrict, reqVO.getDistrict())
//                .eqIfPresent(HousingResourceDO::getLongitude, reqVO.getLongitude())
//                .eqIfPresent(HousingResourceDO::getLatitude, reqVO.getLatitude())
//                .eqIfPresent(HousingResourceDO::getDetailPic, reqVO.getDetailPic())
//                .eqIfPresent(HousingResourceDO::getCoverPic, reqVO.getCoverPic())
//                .eqIfPresent(HousingResourceDO::getBackgroundPic, reqVO.getBackgroundPic())
//                .eqIfPresent(HousingResourceDO::getAuctionState,reqVO.getAuctionState())
//                .betweenIfPresent(HousingResourceDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(HousingResourceDO::getId, HousingResourceDO::getAuctionTime));
    }

    default List<HousingResourceDO> selectList(HousingResourceExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<HousingResourceDO>()
                .likeIfPresent(HousingResourceDO::getTitle, reqVO.getTitle())
                .eqIfPresent(HousingResourceDO::getArea, reqVO.getArea())
                .eqIfPresent(HousingResourceDO::getHouseType, reqVO.getHouseType())
                .eqIfPresent(HousingResourceDO::getHousingType, reqVO.getHousingType())
                .eqIfPresent(HousingResourceDO::getAuctionNumber, reqVO.getAuctionNumber())
                .eqIfPresent(HousingResourceDO::getMarketPrice, reqVO.getMarketPrice())
                .eqIfPresent(HousingResourceDO::getMarketUnitPrice, reqVO.getMarketUnitPrice())
                .eqIfPresent(HousingResourceDO::getPickupSpacePrice, reqVO.getPickupSpacePrice())
                .eqIfPresent(HousingResourceDO::getStartingPrice, reqVO.getStartingPrice())
                .eqIfPresent(HousingResourceDO::getStartingOddPrice, reqVO.getStartingOddPrice())
                .eqIfPresent(HousingResourceDO::getDiscount, reqVO.getDiscount())
                .likeIfPresent(HousingResourceDO::getCellName, reqVO.getCellName())
                .eqIfPresent(HousingResourceDO::getStep, reqVO.getStep())
                .eqIfPresent(HousingResourceDO::getFloor, reqVO.getFloor())
                .eqIfPresent(HousingResourceDO::getPropertyType, reqVO.getPropertyType())
                .eqIfPresent(HousingResourceDO::getNumberOfAuctions, reqVO.getNumberOfAuctions())
                .eqIfPresent(HousingResourceDO::getEarnestMoney, reqVO.getEarnestMoney())
                .eqIfPresent(HousingResourceDO::getBidIncrement, reqVO.getBidIncrement())
                .eqIfPresent(HousingResourceDO::getDiscountRate, reqVO.getDiscountRate())
                .eqIfPresent(HousingResourceDO::getArchitectureYear, reqVO.getArchitectureYear())
                .eqIfPresent(HousingResourceDO::getDecorateSituation, reqVO.getDecorateSituation())
                .likeIfPresent(HousingResourceDO::getAddress, reqVO.getAddress())
                .eqIfPresent(HousingResourceDO::getDistrict, reqVO.getDistrict())
                .eqIfPresent(HousingResourceDO::getLongitude, reqVO.getLongitude())
                .eqIfPresent(HousingResourceDO::getLatitude, reqVO.getLatitude())
                .eqIfPresent(HousingResourceDO::getDetailPic, reqVO.getDetailPic())
                .eqIfPresent(HousingResourceDO::getCoverPic, reqVO.getCoverPic())
                .eqIfPresent(HousingResourceDO::getBackgroundPic, reqVO.getBackgroundPic())
                .eqIfPresent(HousingResourceDO::getIsHutai, reqVO.getIsHutai())
                .betweenIfPresent(HousingResourceDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(HousingResourceDO::getId));
    }

    String getSslURL(@Param("key") String key);

    void updateHousingMobile(@Param("mobile")String mobile);

    String getHousingMobile();

    void deleteByHousingId(@Param("id") Long id);

    String getHousingUpdateTime(@Param("type") String type);
}
